<template>
  <div class="user-container">
    <el-form ref="form" :inline="true" :model="form" label-width="40px">
        <el-form-item label="账号">
            <el-input v-model="form.userName" placeholder="输入账号搜索" clearable>
                <el-button slot="append" icon="el-icon-search" @click="searchByUserName"></el-button>
            </el-input>
        </el-form-item>  
    </el-form>
    <el-button icon="el-icon-plus" type="primary" @click="addCount">添加</el-button>
    <!-- <el-button icon="el-icon-edit" type="info" @click="editCount">修改</el-button> -->
    <!-- <el-button icon="el-icon-delete" type="warning" @click="delCount">删除</el-button> -->
    <div class="user-table">
        <el-table
          ref="multipleTable"
          :data="userList"
          border
          stripe
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
          type="index"
          width="40">
          </el-table-column>
        <!-- <el-table-column
        type="selection"
        width="55">
        </el-table-column> -->
        <el-table-column
          prop="id"
          label="id">   
          </el-table-column>
        <el-table-column
          prop="password"
          label="密码">   
          </el-table-column>
        <el-table-column
          prop="username"
          label="账号">   
          </el-table-column>
        <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="editCount(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="delCount(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <div class="user-pagination">
    <el-pagination
    background
    layout="prev, pager, next"
    :total="totalItem">
    </el-pagination>
  </div>
  <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" center>
  <el-form ref='dialogForm' :model="dialogForm" label-width="40px">
    <el-form-item label="账号">
      <el-input v-model="dialogForm.userCount" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="密码">
      <el-input v-model="dialogForm.userPwd" auto-complete="off"></el-input>
    </el-form-item>
    <el-form-item label="角色">
      <el-input v-model="dialogForm.userRole" auto-complete="off"></el-input>
    </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="handleCancelSubmit">取 消</el-button>
    <el-button type="primary" @click="handleDialogSubmit">确 定</el-button>
  </div>
</el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'name',
      'roles'
    ])
  },
  data() {
      return {
          form:{
              userName:''
          },
          dialogForm:{
              userCount:'',
              userPwd:'',
              userRole:''
          },
          tableData3: [{
            date: '2016-05-03',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-02',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-04',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-01',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-08',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-06',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
            }, {
            date: '2016-05-07',
            name: '王小虎',
            address: '上海市普陀区金沙江路 1518 弄'
        }],
        multipleSelection: [],
        dialogTitle:'添加账户',
        dialogFormVisible:false,
        userList:[],
        page:1,
        rows:10,
        totalItem:0,
        currentDialogType:''
      }
  },
  created(){
    this.$store.dispatch('GetUserList',{page: this.page,rows: this.rows}).then(res => {
      if(res.result === 'success'){
        this.totalItem = res.data.total;
        this.userList = res.data.rows;
      }
    });
  },
  methods:{
      searchByUserName() {
        this.$store.dispatch('getUserByName',{username:this.form.userName}).then(res=>{
          
          if(res.result === 'success' && res.data){
            let _data = res.data;
            // console.log(_data);
            this.totalItem = _data.total;
            this.userList = _data.rows;
            // 暂时没有数据
          }
        }).catch(err=>{
          console.log(err);
          // console.log('res')
          this.$message({
            type: 'info',
            message: '没有相关结果'
          });  
        })
      },
      handleSelectionChange() {

      },
      addCount(index, data) {
          this.dialogFormVisible = true;
          this.dialogTitle = '添加账户';
          this.currentDialogType = 'add';
      },
      editCount(index, data) {
          this.currentDialogType = 'edit';
          this.dialogFormVisible = true;
          this.dialogTitle = '编辑账户';
          this.$store.dispatch('GetSingleUser', {id: data.id}).then(res=>{
            let data = res.data;
            if(res.result === 'success'){
              this.dialogForm.userCount = data.username;
              this.dialogForm.userPwd = data.password;
              this.dialogForm.userRole = data.userRoleList;
            }
          })
      },
      delCount( index ,data ) {
        this.$confirm('确认删除该账户？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('DeleteUser',{id: data.id}).then(res=>{
            this.$store.dispatch('GetUserList',{page: this.page,rows: this.rows});
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
          }).catch(err=>{
            console.log(err);
          })
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      handleDialogSubmit(){
        this.$refs.dialogForm.validate((valid)=>{
          if(valid){
            if(this.currentDialogType === 'add'){
              this.$store.dispatch('AddUser',{
                username: this.dialogForm.userCount,
                password: this.dialogForm.userPwd,
                roleId: this.dialogForm.userRole
              }).then(res=>{
                this.$store.dispatch('GetUserList',{page: this.page,rows: this.rows});
                this.clearDialogForm();
              }).catch(err=>{
                console.log(err)
              })
            }else {
              this.$store.dispatch('UpdateUser',{
                username: this.dialogForm.userCount,
                password: this.dialogForm.userPwd,
                roleId: this.dialogForm.userRole
              }).then(res=>{
                this.$store.dispatch('GetUserList',{page: this.page,rows: this.rows});
                this.clearDialogForm();
              }).catch(err=>{
                console.log(err)
              })
            }
            
          }
        })
      },
      handleCancelSubmit(){
        this.clearDialogForm();
      },
      clearDialogForm(){
        this.dialogFormVisible = false;
        this.dialogForm.userCount = "";
        this.dialogForm.userPwd = "";
        this.dialogForm.userRole = "";
        this.currentDialogType = '';
      }

  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.user {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 46px;
  }
  &-pagination {
      margin-top:50px;
  }
  &-table {
      margin-top:20px;
  }
}
</style>